package com.click369.mianshiqingbao.controller;

import com.click369.mianshiqingbao.pojo.LoginBean;
import com.click369.mianshiqingbao.pojo.UserBean;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.service.LoginService;
import com.click369.mianshiqingbao.service.UserService;
import com.click369.mianshiqingbao.util.MianShiQingBaoHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户登录信息的控制器类
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;
    /**
     * 判断用户登陆信息
     * @param //userbean 用户信息对象
     * @return 成功或失败的提示信息封装类对象
     */
    @RequestMapping(value = "/login.do")
    @ResponseBody
    public PageResult login(@RequestBody UserBean userbean){
        System.out.println("userbean"+userbean.getTelNumber());
        System.out.println("userbean"+userbean.getUserPass());
        List<UserBean> userBeanList = new ArrayList<UserBean>();
        if (!MianShiQingBaoHelper.panDuanPhoneNum(userbean.getTelNumber())) {
            return new PageResult(userBeanList.size(),userBeanList);
        }
        if (userbean.getUserPass().equals("")||userbean.getUserPass().length()<=0) {
            return new PageResult(userBeanList.size(),userBeanList);
        }
        try {

            UserBean userBean = userService.login(userbean.getTelNumber(),userbean.getUserPass());
            if(userBean!=null){
                LoginBean loginBean = new LoginBean();
                loginBean.setUserId(userBean.getUserId());
                loginBean.setLoginTime(new Date());
                loginBean.setLogRel("3");
                loginBean.setLogRelSignal(userbean.getTelNumber());
                loginService.add(loginBean);
                userBeanList.add(userBean);
                return new PageResult(userBeanList.size(),userBeanList);
            }else{
                return new PageResult(userBeanList.size(),userBeanList);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(userBeanList.size(),userBeanList);
        }
    }
}
